﻿#ifndef MAINFACE_H
#define MAINFACE_H

#include <QWidget>
#include <QTcpSocket>
#include <QCloseEvent>
//#include <QPainter>
#include <QMutex>
#include <QProgressBar>
#include <QListWidgetItem>

namespace Ui {
class MainFace;
}

struct UpLoadProcess
{
    QString filename;
    int chunksize;
};

struct DownLoadProcess
{
    QString filename;
    int chunksize;
};

struct FileInfo
{
    QString fileName;
    QString fileSize;
    QString fileType;
    QString Time;
};

struct TransferFileInfo
{
    QString fileName;
    QString Type;
    int chunkIndex;
    int totalChunks;
};

class MainFace : public QWidget
{
    Q_OBJECT

public:
    explicit MainFace(QWidget *parent = nullptr);
    ~MainFace();

    // 发送到服务器
    void WriteToServer(const QJsonObject &obj);

    void process(const QByteArray &data);

    // 显示文件信息
    void ShowFileInfo(QVector<FileInfo> &result);

    // 显示传输信息
    void ShowTransferInfo(QVector<TransferFileInfo> &result);

public slots:
    void GetSocketAndName(QTcpSocket *socket, const QString &fromname); // 获取套接字、名字和断点

protected:
    void closeEvent(QCloseEvent * event)
    {
        emit closeFace();  // 关闭时发射信号
    }

//    void paintEvent(QPaintEvent *event) override
//    {
//        QPainter painter(this);
//        QPixmap background(":/img/1.jpg");
//        painter.drawPixmap(0, 0, width(), height(), background);
//        QWidget::paintEvent(event);
//    }

signals:
    void closeFace();

private slots:
    void on_pushButton_all_clicked();

    void on_pushButton_photo_clicked();

    void on_pushButton_video_clicked();

    void on_pushButton_transfer_clicked();

    void on_pushButton_upload_clicked();

    void on_pushButton_download_clicked();

    void on_pushButton_remove_clicked();

    void handleShowInfo(const QString &filename);

    void handleShowTransferInfo();

    void handleUploadProgress(const QString& filename, int progress); // 新增槽函数，处理上传进度更新

    void on_listWidget_itemClicked(QListWidgetItem *item);

private:
    Ui::MainFace *ui;
    QTcpSocket *socket;
    QString fromname; // 记录登录的人
    QVector<UpLoadProcess> UpLoadInfo;
    QVector<DownLoadProcess> DownLoadInfo;
    QMap<QString, int> uploadProgress; // 文件名 -> 已上传的块数
    QMap<QString, int> downloadProgress; // 文件名 -> 已上传的块数
    QMutex uploadProgressMutex; // 互斥锁
    QMutex downloadProgressMutex; // 互斥锁
    QMap<QString, QProgressBar*> progressBars; // 文件名 -> 进度条指针
    QList<QString> selectFile;
};

#endif // MAINFACE_H
